#include <iostream>

using std::cout;
using std::endl;

class Point
{
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
        cout << "Point(int = 0, int = 0)" << endl;
    }

    //默认情况下，编译器会自动生成拷贝构造函数
    Point(const Point &rhs)
    : _ix(rhs._ix)
    , _iy(rhs._iy)
    {
        cout << "Point(const Point &)" << endl;
    }

    void print()
    {
        cout << "(" << _ix
             << ", " << _iy
             << ")" << endl;
    }

    //默认情况下，编译器也会自动生成析构函数
    ~Point()
    {
        cout << "~Point()" << endl;
    }

private:
    int _ix;
    int _iy;
};

//2、当函数的参数是类类型的时候，在进行形参与实参结合的时候
//会调用拷贝构造函数
void func(Point rhs)//Point rhs = pt
{
    cout << "rhs = ";
    rhs.print();
}

Point func2()
{
    Point pp(1, 2);
    cout << "pp = ";
    pp.print();

    return pp;
}

void test()
{
    int a = 10;
    int b = a;

    Point pt(1, 2);//pt栈对象
    cout << "pt = ";
    pt.print();

    cout << endl;
    //拷贝(复制)构造函数
    //1、用一个已经存在的对象去初始化一个刚刚创建的对象，那么
    //会调用拷贝构造函数
    Point pt2 = pt;//pt2也是栈对象
    cout << "pt2 = ";
    pt2.print();
}

void test2()
{
    Point pt(10, 20);
    cout << "pt = ";
    pt.print();

    cout << endl;
    func(pt);
}

void test3()
{
    Point pt = func2();
    cout <<"pt = ";
    pt.print();
}

int main(int argc, char *argv[])
{
    test3();
    return 0;
}

